from fastapi import FastAPI
from models import Icon, Icon_Pydantic, IconIn_Pydantic
from config import db_config
from tortoise.contrib.fastapi import register_tortoise

app = FastAPI()

register_tortoise(
  app,
  config=db_config,
  modules={"models": ["models"]},
  generate_schemas=False
)

@app.get("/")
async def root():
  return { "msg": "Hello FastAPI" }

@app.get("/icons")
async def get_icons():
  icons = await Icon.all()
  return { "status": 200, "msg": "Success", "data": icons }

@app.get("/icon/{id}")
async def get_icon(id: int):
  icon = await Icon.get(id=id)
  return { "status": 200, "msg": "Success", "data": icon }

@app.put("/icon/{id}", response_model=Icon_Pydantic)
async def update_icon(id: int, icon: IconIn_Pydantic):
  await Icon.filter(id=id).update(**icon.dict(exclude_unset=True))
  return await Icon_Pydantic.from_queryset_single(Icon.get(id=id))


if __name__ == '__main__':
  import uvicorn
  uvicorn.run('main:app', debug=True, reload=True)
